<template>
  <div>
    <!-- 标题 -->
    <el-dialog
      :username="info.idAdd ? '添加管理员' : '编辑管理员'"
      :before-close="cancel"
      :visible.sync="info.isShow"
      center
    >
      <el-form :model="mangerForm" ref="manger" :rules="rules">
        <!--  多选 -->
        <el-form-item prop='roleid' label="所属角色" :label-width="formLabelWidth">
          <el-select v-model="mangerForm.roleid" placeholder="请选择">
            <el-option  :value="0" disabled>==请选择==</el-option>
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!--  管理员名称-->
        <el-form-item
          label="管理员名称"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="mangerForm.username" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="mangerForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="mangerForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="addInfo" v-if="info.isAdd" type="primary"
          >添加</el-button
        >
        <el-button @click="editInfo" v-else type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapActions, mapGetters } from "vuex";
// 
import { userAdd, userInfo, userEdit } from "../../util/request/api";
export default {
  props: ["info"],
  data() {
    return {
      formLabelWidth: "120px",
      //   表单的数据
      mangerForm: {
        roleid: 0,
        username: "",
        password: "",
        status: 1,
      },
      // options: [
      //   {
      //     value: "1",
      //     label: "系统管理",
      //   },
      //   {
      //     value: "2",
      //     label: "商城管理",
      //   },
      // ],
      rules: {
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
        roleid: [
          { required: true, message: "请选择所属角色", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      rolelist: "role/getRoleList",
    }),
  },
  mounted() {
    // 页面加载时就调用角色列表
    this.getRolelist()
  },
  methods: {
    // 重新获取列表
    // getMenuList() {
    //   menuList().then((res) => {
    //     console.log(res, "管理员列表 ");
    //     // 如果通过，那么就获取数据库里的列表
    //     if (res.code == 200) {
    //       this.tableData = res.list;
    //     }
    //   });
    // },
    ...mapActions({
      getRolelist: "role/getRoleAction",
            getUserlist: "manger/getUserAction",
      getCount: "manger/getCountAction",
    }),
    //   关闭弹窗
    cancel() {
      // 子传父
      this.$emit("cancel", false);
      // 不可以是双引号！！！！！！！！！！！！！！！
      // this.$emit('cancel',false)
      this.empty();
    },
    // 清空弹窗
    empty() {
      this.mangerForm = {
         roleid: 0,
        username: "",
        password: "",
        status: 1,
      };
    },
    // 为列表添加一条信息
    addInfo() {
      // console.log(this.mangerForm);
      this.$refs.manger.validate((valid) => {
        if (valid) {
          // 调取接口
          console.log(this.mangerForm);

          userAdd(this.mangerForm).then((res) => {
            console.log(res, "管理员");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              // 刷新一次，更新列表
              // location.reload(),单页应用不使用
              this.getUserlist();
              this.getCount();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
    // 获取一条数据e
    lookup(uid) {
      console.log(uid, "每一条数据的主key");
      userInfo({ uid }).then((res) => {
        console.log(res, "每一条数据的主key");
        this.mangerForm = res.list;
        this.mangerForm.uid = uid

      });
    },
    // 编辑
    editInfo() {
      // console.log(this.mangerForm);
      this.$refs.manger.validate((valid) => {
        if (valid) {
          // 调取接口
          console.log(this.mangerForm);

          userEdit(this.mangerForm).then((res) => {
            console.log(res, "管理员");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              // 刷新一次，更新列表
              // location.reload(),单页应用不使用
              this.getUserlist();
              this.getCount();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
  },
};
</script>